package bll

import (
	"context"
	"maya-service/model/po"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type aiConsultStatics struct {
	iAiConsultStatics store.IAiConsultStatics
}

var AiConsultStatics = &aiConsultStatics{
	iAiConsultStatics: mysql.AiConsultStatics,
}

func init() {
	Register(AiConsultStatics)
}

func (a *aiConsultStatics) init() func() {
	return func() {}
}

func (a *aiConsultStatics) AiConsultStatics(ctx context.Context, in *po.AiConsultStaticsReq) error {
	findParams := &model.AiConsultStaticsInfoRequest{
		UserId: in.UserId,
		Level:  in.Level,
		Module: in.Module,
	}

	// 结果
	find, _ := a.Find(ctx, findParams)
	if find != nil && find.Id > 0 {
		count := find.Count + in.CountDelta
		totalScore := find.TotalScore + in.Score
		maxScore := find.MaxScore
		if in.Score > find.MaxScore {
			maxScore = in.Score
		}

		minScore := in.Score
		if find.MinScore > 0 && in.Score > find.MinScore {
			minScore = find.MinScore
		}
		return a.Update(ctx, &model.AiConsultStaticsUpdateRequest{
			Id:         find.Id,
			Count:      &count,
			TotalScore: &totalScore,
			MaxScore:   &maxScore,
			MinScore:   &minScore,
		})
	}

	createData := &model.AiConsultStaticsCreateRequest{
		UserId:     in.UserId,
		Level:      in.Level,
		Module:     in.Module,
		Count:      in.CountDelta,
		TotalScore: in.Score,
		MaxScore:   in.Score,
		MinScore:   in.Score,
	}

	return a.Create(ctx, createData)
}

// Create
func (a *aiConsultStatics) Create(ctx context.Context, in *model.AiConsultStaticsCreateRequest) error {
	var (
		err error
	)
	c := buildAiConsultStatics(in)
	_, err = a.iAiConsultStatics.Create(ctx, c)
	return err
}

// Update
func (a *aiConsultStatics) Update(ctx context.Context, in *model.AiConsultStaticsUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.UserId != nil {
		dict["user_id"] = in.UserId
	}

	if in.Level != nil {
		dict["level"] = in.Level
	}

	if in.Module != nil {
		dict["module"] = in.Module
	}

	if in.Count != nil {
		dict["count"] = in.Count
	}

	if in.TotalScore != nil {
		dict["total_score"] = in.TotalScore
	}

	if in.MaxScore != nil {
		dict["max_score"] = in.MaxScore
	}

	if in.MinScore != nil {
		dict["min_score"] = in.MinScore
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iAiConsultStatics.Update(ctx, in.Id, dict)
}

// Delete
func (a *aiConsultStatics) Delete(ctx context.Context, in *model.AiConsultStaticsDeleteRequest) error {
	return a.iAiConsultStatics.Delete(ctx, in.Id)
}

// List
func (a *aiConsultStatics) List(ctx context.Context, in *model.AiConsultStaticsListRequest) (*model.AiConsultStaticsListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.AiConsultStatics
		out   = &model.AiConsultStaticsListResponse{}
	)

	if total, list, err = a.iAiConsultStatics.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.AiConsultStaticssEntityToDto(list)

	return out, nil
}

// Find
func (a *aiConsultStatics) Find(ctx context.Context, in *model.AiConsultStaticsInfoRequest) (*model.AiConsultStaticsInfo, error) {
	var (
		err  error
		data *entity.AiConsultStatics
		out  = &model.AiConsultStaticsInfo{}
	)

	if data, err = a.iAiConsultStatics.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.AiConsultStaticsEntityToDto(data)
	return out, nil
}

// buildAiConsultStatics build entity
func buildAiConsultStatics(in *model.AiConsultStaticsCreateRequest) *entity.AiConsultStatics {

	ety := &entity.AiConsultStatics{}

	ety.UserId = in.UserId

	ety.Level = in.Level

	ety.Module = in.Module

	ety.Count = in.Count

	ety.TotalScore = in.TotalScore

	ety.MaxScore = in.MaxScore

	ety.MinScore = in.MinScore

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
